﻿/*---------------------------------------------------------------------*/
// fileName="DirectoryWinPresenter.cs" CompanyName="SureKAM"
// Copyright SureKAM (c). All right reserved.
/*---------------------------------------------------------------------*/

using SureKAM.BackUp.Notify.Domain;

namespace SureKAM.BackUp.Notify.Presenter
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using SureKAM.BackUp.Notify.Presenter.Interfaces;
    using SureKAM.BackUp.Notify.Presenter.ViewInterfaces;
    using SureKAM.BackUp.Notify.Service.Interfaces;

    public class DirectoryWinPresenter : IDirectoryWinPresenter
    {
        #region 私有字段
        private IDirectoryWinView iDirectoryWinView;
        private IDirectoryService iDirectoryService;
        #endregion

        #region 构造
        public DirectoryWinPresenter(IDirectoryWinView iDirectoryWinView, IDirectoryService iDirectoryService)
        {
            this.iDirectoryService = iDirectoryService;
            this.iDirectoryWinView = iDirectoryWinView;
        }
        #endregion

        #region 私有字段
        /// <summary>
        /// 刷新
        /// </summary>
        /// <returns></returns>
        private List<Notify.Domain.Directory> reflash()
        {
            List<Notify.Domain.Directory> directories = this.iDirectoryService.GetDirectories();
            return directories;
        }

        /// <summary>
        /// 添加一个新对象
        /// </summary>
        /// <param name="name"></param>
        /// <param name="size"></param>
        /// <param name="serverName"></param>
        /// <param name="address"></param>
        /// <returns></returns>
        private bool Add(string name, int size, string serverName, string address)
        {
            Notify.Domain.Directory directory = new Directory();
            directory.ID = Guid.NewGuid().ToString();
            directory.Name = name;
            directory.MaxSize = size;
            directory.BackUpServer = serverName;
            directory.Address = address;
            directory.CreateTime = DateTime.Now;
            if (String.IsNullOrEmpty(this.iDirectoryService.Add(directory))) return false;
            return true;
        }

        /// <summary>
        /// 删除一个新对象
        /// </summary>
        /// <param name="currentId"></param>
        /// <returns></returns>
        private bool Delete(string  currentId)
        {
            Domain.Directory directory = this.iDirectoryService.GetDirectoryById(currentId);
            if (String.IsNullOrEmpty(this.iDirectoryService.Delete(directory))) return false;
            return true;
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="directory"></param>
        /// <returns></returns>
        private bool  Update(Notify.Domain.Directory directory)
        {
            if (this.iDirectoryService.Update(directory) != null) return true;
            return false;
        }
        #endregion

        #region 公有方法
        public void Init()
        {
            List<Notify.Domain.Directory> directories = this.iDirectoryService.GetDirectories();
            this.iDirectoryWinView.DataBindDirectiry(directories);
            this.iDirectoryWinView.reflash = reflash;
            this.iDirectoryWinView.directoryAdd = Add;
            this.iDirectoryWinView.directoryDelete = Delete;
            this.iDirectoryWinView.directoryUpdate = Update;
        }
        #endregion
    }
}
